﻿using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DevExpress.DXperience.Demos.CodeDemo.Data;
using DevExpress.XtraGrid;
using DevExpress.XtraEditors;

namespace TableModule
{
    public partial class FormTableSetting : DevExpress.XtraEditors.XtraForm
    {
        public FormTableSetting()
        {
            InitializeComponent();
        }

        private void FormTableSetting_Load(object sender, EventArgs e)
        {
            
            //TableManager.tableSystemSetting.tableSetting.tableAxisSettings;
            gridControlTableSetting.DataSource = TableManager.tableSystemSetting.tableSetting.tableAxisSettings;
            gridControlTableDataPosProject.DataSource = TableManager.tableData.TableSingleData.listTableDataPos;
            gridControlTableDataPosGlobal.DataSource = TableManager.globaltableData.TableSingleData.listTableDataPos;
            gridControlTableDataSpdGloabal.DataSource = TableManager.globaltableData.TableSingleData.listTableDataSpeed;
            gridControlTableDataToolGloabal.DataSource = TableManager.globaltableData.TableSingleData.listTableDataTool;
            gridControlTableDataLocalGloabal.DataSource= TableManager.globaltableData.TableSingleData.listTableDataLocal;

            GridView gridView = gridControlTableDataPosProject.MainView as GridView;
            gridView.InitNewRow += GridView_InitNewRow;

            gridControlTableDataPosProject.ProcessGridKey += GridControlTableDataPosProject_ProcessGridKey;
            gridControlTableDataPosGlobal.ProcessGridKey += GridControlTableDataPosProject_ProcessGridKey;
            gridControlTableDataSpdGloabal.ProcessGridKey += GridControlTableDataPosProject_ProcessGridKey;
            gridControlTableDataToolGloabal.ProcessGridKey += GridControlTableDataPosProject_ProcessGridKey;
            gridControlTableDataLocalGloabal.ProcessGridKey += GridControlTableDataPosProject_ProcessGridKey;


        }

        private void GridControlTableDataPosProject_ProcessGridKey(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && e.Modifiers == Keys.Control)
            {
                if (XtraMessageBox.Show("Delete row(s)?", "Delete rows dialog", MessageBoxButtons.YesNo) !=
                  DialogResult.Yes)
                    return;
                GridControl grid = sender as GridControl;
                GridView view = grid.FocusedView as GridView;
                //Deletes the selected rows in multiple selection mode or focused row
                //  in single selection mode. 
                view.DeleteSelectedRows();
            }

        }

        private void GridView_InitNewRow(object sender, InitNewRowEventArgs e)
        {
            //throw new NotImplementedException();
        }

        private void FormTableSetting_FormClosing(object sender, FormClosingEventArgs e)
        {
            TableManager.tableData.TableSingleData.dicTableDataPos = TableManager.tableData.TableSingleData.listTableDataPos.ToDictionary(p => p.Name);
            TableManager.globaltableData.TableSingleData.dicTableDataPos = TableManager.globaltableData.TableSingleData.listTableDataPos.ToDictionary(p => p.Name);
            TableManager.globaltableData.TableSingleData.dicTableDataLocal = TableManager.globaltableData.TableSingleData.listTableDataLocal.ToDictionary(p => p.Name);
            TableManager.globaltableData.TableSingleData.dicTableDataSpeed = TableManager.globaltableData.TableSingleData.listTableDataSpeed.ToDictionary(p => p.Name);
            TableManager.globaltableData.TableSingleData.dicTableDataTool = TableManager.globaltableData.TableSingleData.listTableDataTool.ToDictionary(p => p.Name);
        }
    }
}
